package cn.gan.loveofsoul.service.impl;

import cn.gan.loveofsoul.commons.result.ErrorMessage;
import cn.gan.loveofsoul.commons.result.Result;
import cn.gan.loveofsoul.commons.result.StatusCode;
import cn.gan.loveofsoul.commons.utils.UUIDUtil;
import cn.gan.loveofsoul.entity.PhotoFile;
import cn.gan.loveofsoul.entity.PhotoUploadRecord;
import cn.gan.loveofsoul.entity.vo.PhotoUploadRecordVO;
import cn.gan.loveofsoul.exception.BusinessException;
import cn.gan.loveofsoul.mapper.PhotoUploadRecordMapper;
import cn.gan.loveofsoul.service.IPhotoFileService;
import cn.gan.loveofsoul.service.IPhotoUploadRecordService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ganwh
 * @since 2023-10-26
 */
@Service
@Log4j2
public class PhotoUploadRecordServiceImpl extends ServiceImpl<PhotoUploadRecordMapper, PhotoUploadRecord> implements IPhotoUploadRecordService {

    @Autowired
    private IPhotoFileService photoFileService;

    @Autowired
    private PhotoUploadRecordMapper photoUploadRecordMapper;

    /**
     * 功能描述: 新增上传记录
     *
     * @param photoUploadRecordVO
     * @return cn.gan.loveofsoul.commons.result.Result
     * @author ganwh 2023/11/8 14:22
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result insertPhotoUploadRecord(PhotoUploadRecordVO photoUploadRecordVO) {
        try {
            photoUploadRecordVO.setImageCount(CollectionUtil.size(photoUploadRecordVO.getPhotoFileList()));
            PhotoUploadRecord newPhotoUploadRecord = BeanUtil.copyProperties(photoUploadRecordVO, PhotoUploadRecord.class);
            newPhotoUploadRecord.setGroupId(UUIDUtil.getUUID32Bits());
            newPhotoUploadRecord.setUploadTime(LocalDateTime.now());
            this.save(newPhotoUploadRecord);

            List<PhotoFile> photoFileList = photoUploadRecordVO.getPhotoFileList();
            photoFileList.forEach(photoFile -> {
                photoFile.setGroupId(newPhotoUploadRecord.getGroupId());
            });
            photoFileService.saveBatch(photoFileList);

            return new Result(StatusCode.SUCCESS, ErrorMessage.SUCCESS);
        } catch (Exception e) {
            log.error("新增上传记录失败 ->", e);
            throw new BusinessException(StatusCode.ERROR, ErrorMessage.SYSTEM_ERROR);
        }
    }

    /**
     * 功能描述: 分页查询相册上传记录
     *
     * @param pageIndex
     * @param pageSize
     * @param albumId
     * @return cn.gan.loveofsoul.commons.result.Result
     * @author ganwh 2023/11/8 18:00
     */
    public Result pageQueryPhotoUploadRecord(Integer pageIndex, Integer pageSize, Integer albumId) {
        Page<PhotoUploadRecord> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<PhotoUploadRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PhotoUploadRecord::getAlbumId, albumId);
        page = photoUploadRecordMapper.selectPage(page, wrapper);
        return new Result(StatusCode.SUCCESS, ErrorMessage.SUCCESS, page);
    }

    @Override
    public Result deletePhotoUploadRecord(Integer id) {
        PhotoUploadRecord photoUploadRecord = photoUploadRecordMapper.selectById(id);
        getOptById(id);
        return null;
    }
}
